package com.sh.caryr.erp.entity.basis;

import com.sh.caryr.erp.custmz.UserAuthorityContainer;
import com.sh.caryr.erp.entity.enums.Page;
import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.util.ERPConstants;

import java.util.ArrayList;
import java.util.List;

public class MenuItem {
    public MenuItem parent;
    public MenuItem child;
    public MenuItem next;
    public String href;
    public String title;  //Default text shows up when i18n not load successfully.
    public String msgClass;
    public String icon;

    public MenuItem(String href, String title, String msgClass, String icon) {
        this.href = href;
        this.title = title;
        this.icon = icon;
        this.msgClass = msgClass;
    }

    public static List<MenuItem> getMenus(User user) {
        String authStr = UserAuthorityContainer.convertUserAuthToString(user);
        boolean superUser = ERPConstants.SYS_SUPER_USERNAME.equals(user.getName()) || user.getIsSuperuser() == ERPConstants.ENITYT_ACTIVE ? true : false;
        String target = "";

        List<MenuItem> items = new ArrayList<>();
        MenuItem item = new MenuItem(Page.DASHBOARD.getVal(), "控制台", "msg_home", "icon-home"); // home
        items.add(item);

        item = new MenuItem("javascript:;", "产品", "msg_product", "fa fa-cube"); // Product
        target = "product";
        if (superUser || authStr.contains(target + "_read")) {
            item.addChild(new MenuItem(Page.PRODUCT.getVal(), "产品信息", "msg_title_product", "fa fa-barcode"));
            item.addChild(new MenuItem(Page.PRODUCT_MAPPING.getVal(), "产品对接", "msg_productMapping", "fa fa-link"));
            item.addChild(new MenuItem("#", "*产品属性", "msg_title_attr", "fa fa-adjust"));
            items.add(item);
        }

        target = "stock";
        if (superUser || authStr.contains(target + "_read")) {
            item = new MenuItem("javascript:;", "库存", "msg_title_sku", "fa fa-cubes"); // Material
            item.addChild(new MenuItem("stock", "库存管理", "msg_stock_management", "fa fa-cubes"));
            item.addChild(new MenuItem("skuRecord", "修改记录", "msg_modify_his", "icon-layers"));
            item.addChild(new MenuItem("#", "*库存警戒", "msg_stock_alarm", "fa fa-bell"));
            items.add(item);
        }

        target = "salesOrder";
        if (superUser || authStr.contains(target + "_read")) {
            item = new MenuItem("javascript:;", "销售", "msg_order", "fas fa-file-alt"); // Sales
            item.addChild(new MenuItem(Page.PAGE_ORDERS.getVal(), "销售订单", "msg_order_list", "fas fa-file-alt"));
            if (superUser || (authStr.contains(target + "_" + ERPConstants.AUTH_ACTVT_CREATE) &&
                    (authStr.contains(target + "_" + ERPConstants.AUTH_ACTVT_IMPORT)))) {
                item.addChild(new MenuItem(Page.PAGE_AS_ORDERS.getVal(), "售后服务", "msg_srv_order", "fas fa-phone-square"));
                item.addChild(new MenuItem("importTrades", "批量导入", "msg_order_upload", "fa fa-upload"));
            }
            items.add(item);
        }

        target = "user";
        if (superUser || authStr.contains("user_read") || authStr.contains("userRole_read")) {
            item = new MenuItem("javascript:;", "用户", "msg_user", "icon-users"); // User
            if (superUser || authStr.contains(target + "_read")) {
                item.addChild(new MenuItem(target, "用户管理", "msg_user_mag", "fa fa-user"));
            }
            target = "userRole";
            if (superUser || authStr.contains(target + "_read")) {
                item.addChild(new MenuItem(target, "权限管理", "msg_user_auth_mag", "icon-eye"));
            }
            items.add(item);
        }

        item = new MenuItem("javascript:;", "配置", "msg_cfg", "fa fa-cog"); // User
        target = "shop";
        if (superUser || authStr.contains(target + "_read")) {
            item.addChild(new MenuItem(target, "店铺管理", "msg_shop_manage", "fa fa-archive"));
        }
        item.addChild(new MenuItem("#", "*系统日志", "msg_system_log", "icon-layers"));
        item.addChild(new MenuItem("#", "*系统设置", "msg_system_cfg", "icon-puzzle"));
        items.add(item);

        return items;
    }

    public void addChild(MenuItem child) {
        child.parent = this;
        if (this.child == null) {
            this.child = child;
            return;
        }
        MenuItem lastChild = this.child;
        while (lastChild.next != null) {
            lastChild = lastChild.next;
        }
        lastChild.next = child;
    }

    public static String menuHtml(List<MenuItem> items, String currentUrl) {
        String menuHtml = "";
        if (items == null || items.isEmpty()) {
            return menuHtml;
        }

        for (MenuItem item : items) {
            menuHtml += parentHtml(item, currentUrl);
        }
        return menuHtml;
    }

    private static String parentHtml(MenuItem item, String currentUrl) {
        String childrenHtml = childrenHtml(item.child, currentUrl);
        String menuArrow = "", open = "";
        if ("".equals(childrenHtml)) {
            menuArrow = "";
            open = "";
        } else {
            if (match(item.child, currentUrl)) {
                open = " open active ";
            }
            menuArrow = "<span class='arrow " + open + "'></span>";
        }
        String parentTemplate =
                "<li class='nav-item " + open + "'>" +
                        "<a class='nav-link nav-toggle' href='" + item.href + "'>" +
                        "<i class='" + item.icon + "'></i>" +
                        "<span class='title " + item.msgClass + "'>" + item.title + "</span>" +
                        menuArrow +
                        "</a>" +
                        childrenHtml +
                        "</li>";
        return parentTemplate;
    }

    private static String childrenHtml(MenuItem item, String currentUrl) {
        StringBuilder html = new StringBuilder();
        String open;
        if (item == null) {
            return html.toString();
        }

        html.append("<ul class='sub-menu'>");

        MenuItem temp = item;
        while (temp != null) {

            if (currentUrl.equals(temp.href)) {
                open = " open active ";
            } else {
                open = "";
            }
            html.append("<li class='nav-item  ").append(open).append("'>")
                    .append("<a href='").append(temp.href).append("' class='nav-link'>")
                    .append("<i class='").append(temp.icon).append("'></i>")
                    .append("<span class='title ").append(temp.msgClass).append("'>").append(temp.title).append("</span>")
                    .append("</a>")
                    .append("</li>");
            temp = temp.next;
        }
        html.append("</ul>");
        return html.toString();
    }


    private static boolean match(MenuItem item, String currentUrl) {
        MenuItem temp = item;
        while (temp != null) {
            if (currentUrl.equals(temp.href)) {
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

}
